package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.CurrentMember;
import org.darkforest.common.services.GiftService;
import org.darkforest.dao.mapper.OrderGiftCodeMapper;
import org.darkforest.model.*;
import org.darkforest.auth.AuthPassport;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2017/4/26.
 */
@RestController
@RequestMapping(value = "gift")
@Api(value = "gift", description = "礼品",produces= MediaType.APPLICATION_JSON_VALUE)
public class GiftController {
    @Autowired
    private GiftService giftService;
    @Autowired
    private OrderGiftCodeMapper orderGiftCodeMapper;

    @RequestMapping(value = "list" ,method = RequestMethod.GET)
    @AuthPassport
    @ApiOperation(value = "根据订单支付号的礼品二维码", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestEntity list(String orderBatchNumber,@CurrentMember Member member){
        OrderGiftCode orderGiftCode = new OrderGiftCode();
        orderGiftCode.setCreaterId(member.getMemberId());
        orderGiftCode.setGenerateOrderId(orderBatchNumber);
        orderGiftCode.setPageSwitch(false);
        List<OrderGiftCode> orderGiftCodeList = orderGiftCodeMapper.selectByCondition(orderGiftCode);
        return RestEntity.success(orderGiftCodeList);
    }

    @AuthPassport
    @RequestMapping(value = "reqGiftCode",method = RequestMethod.POST)
    @ApiOperation(value = "获取礼品码", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity reqGiftCode(@CurrentMember Member member, @RequestParam(required = true) Integer orderId){
        try{
            AjaxReturnData ajaxReturnData = giftService.reqGiftCode(member.getMemberId(),orderId);
            return RestEntity.success(ajaxReturnData);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @AuthPassport
    @RequestMapping(value = "receiveGift",method = RequestMethod.POST)
    @ApiOperation(value = "领取礼品", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity doReceiveGift(@CurrentMember Member member,@RequestBody Address address){
        try{
            AjaxReturnData ajaxReturnData = giftService.doReceiveGift(address, address.getGiftCode(), member);
            return RestEntity.success(ajaxReturnData);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    //@AuthPassport
    @RequestMapping(value = "checkGiftCode",method = RequestMethod.GET)
    @ApiOperation(value = "检查礼品码是否正常并返回礼品码商品信息", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity checkGiftCode(String giftCode){
       boolean result =  giftService.checkGiftCodeStatus(giftCode);
       if(result){
           List<OrderGoods> goodsList = giftService.listGoodsByGiftCode(giftCode);
           return RestEntity.success(goodsList);
       } else{
           return RestEntity.failed("INVALID GIFTCODE");
       }
    }
}
